package com.itheima.tlias.servies.impl;

import com.itheima.tlias.mapper.DeptMapper;
import com.itheima.tlias.mapper.EmpMapper;
import com.itheima.tlias.pojo.Dept;
import com.itheima.tlias.servies.DeptServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptServices {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List list(){
        List<Dept> deptList = deptMapper.list();
        return deptList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void DeleteById(Integer id) {
        deptMapper.deleteByid(id);
        empMapper.deleteBydeptId(id);
    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    @Override
    public Dept select(Integer id) {
        Dept dept = deptMapper.select(id);
        return dept;
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updata(dept);
    }

}
